<?php
namespace App\Models\Entity\Column;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Column
 * @package App\Models\Entity\Column
 * @version August 1, 2017, 4:32 pm UTC
 */
class Column extends Model
{
    use SoftDeletes;

    public $table = 'columns';

    protected $dates = ['deleted_at'];

    public $fillable = [
        // 'column_no',
        'name',
        'title',
        'subtitle',
        // 'tpl_id',
        'content_template_id',
        'collist_id',
        'img',
        'status',
        'short_desc',
        'long_desc',
        'created_at',
        'updated_at',
        'listorder',
        // 上下线, 发布
        'online_status',
        'moderate_status',
        'publish_status',
        'moderate_by',
        'moderate_at',
        'online_at',
        'offline_at',
        'user_id',
        'publish_down_status',
        // 子数据连接
        'children_data_href',
        // 推荐类型ID
        'recommend_type_id',
        // 推荐数据ID(模板,栏目列表....)
        'recommend_data_id',
        'sp_id',
        'ogroup_id',
        'onfocus',
        'onclick',
        'restful',
        'description',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'column_no'  => 'string',
        'name'                => 'string',
        'title'               => 'string',
        'subtitle'            => 'string',
        // 'tpl_id'     => 'integer',
        'content_template_id' => 'integer',
        'collist_id'          => 'integer',
        'img'                 => 'string',
        'status'              => 'string',
        'short_desc'          => 'string',
        'long_desc'           => 'string',
        'listorder'           => 'integer',
        'children_data_href'  => 'string',
        'recommend_type_id'   => 'integer',
        'recommend_data_id'   => 'integer',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        // 'column_no'  => 'required|max:100|unique:columns,column_no',
        'name'       => 'required|max:100',
        'title'      => 'max:100',
        'subtitle'   => 'max:100',
        'tpl_id'     => 'required|max:200',
        'img'        => 'max:200',
        'online_at'  => 'date',
        'offline_at' => 'date',
        'status'     => 'required',
        'short_desc' => 'required|max:100',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function sp()
    {
        return $this->hasOne(\App\Models\Common\EntityData\Channel\Sp::class, 'id', 'sp_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function ogroup()
    {
        return $this->hasOne(\App\Models\Common\EntityData\Channel\Ogroup::class, 'id', 'ogroup_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     * 类型
     **/
    public function ccats()
    {
        return $this->belongsToMany(\App\Models\Common\CatData\Video\Ccat::class, 'column_ccats', 'column_id', 'ccat_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 模板
     **/
    public function tpl()
    {
        return $this->hasOne(\App\Models\Common\CatData\App\Tpl::class, 'id', 'tpl_id');
    }

    public function contentTemplate()
    {
        return $this->hasOne(\App\Models\Operation\ContentTemplate::class, 'id', 'content_template_id');
    }


       /**
     * @return \Illuminate\Database\Eloquent\Relations\belongToMany
     * 以及栏目添加点播媒资
     **/
    public function vassetis()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Vasseti::class, 'column_vassetis', 'column_id', 'vasseti_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * 二级栏目
     **/
    public function subcolumns()
    {
        return $this->hasMany(\App\Models\Entity\Column\Subcolumn::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     * 栏目列表
     **/
    public function collists()
    {
        return $this->belongsToMany(\App\Models\Entity\Column\Collist::class, 'collist_columns', 'column_id', 'collist_id');
    }

    /**
     * 过滤上下线数据
     **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
     * 过滤上线发布数据
     **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
     * 过滤下线发布数据
     **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }
}
